package org.springblade.pm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.pm.entity.Contract;
import org.springblade.pm.entity.Problem;
import org.springblade.pm.service.IProblemService;
import org.springblade.pm.vo.PageQuery;
import org.springblade.pm.vo.ProblemVo;
import org.springblade.pm.wrappper.ProblemWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("problem")
@AllArgsConstructor
public class ProblemController {

	private IProblemService problemService;

	@GetMapping("/getProblemList")
	@ApiOperation(value = "获取合同列表")
	public R getProblemList(Problem problem, PageQuery pageQuery){
		QueryWrapper<Problem> wrapper = new QueryWrapper<>();
		wrapper.eq("project_id",problem.getProjectId());
		String problemContent = problem.getProblemContent();
		if (problemContent!=null&&!"".equals(problemContent)){
			wrapper.like("problem_content",problemContent);
		}
		Integer problemType = problem.getProblemType();
		if (problemType!=null&&!"".equals(problemType)){
			wrapper.eq("problem_type",problemType);
		}
		IPage<Problem> page = problemService.page(Condition.getPage(pageQuery), wrapper);
		List<ProblemVo> contractVos = ProblemWrapper.build().entityVoList(page.getRecords());
		IPage<ProblemVo> contractVoIPage = ProblemWrapper.build().pageVO(page);
		contractVoIPage.setRecords(contractVos);
		return R.data(contractVoIPage);
	}

	@PostMapping("/submit")
	@ApiOperation(value = "新增问题")
	public R submit(Problem problem, BladeUser bladeUser) {
		if(problem.getId()==null||"".equals(problem.getId())){
			problem.setCreateUser(bladeUser.getUserId());
			problem.setCreateTime(new Date());
		}
		return R.status(problemService.saveOrUpdate(problem));
	}
	@GetMapping("/problemDetails")
	@ApiOperation(value = "查询问题详情")
	public R problemDetails(Integer id) {
		return R.data(problemService.getById(id));
	}

	@GetMapping("/deleteproblem")
	@ApiOperation(value = "删除问题")
	public R deleteproblem(Integer id) {
		return R.status(problemService.removeById(id));
	}
}
